package com.foxitcloud.webpdf.calldll;

import org.apache.commons.lang3.mutable.MutableLong;

import com.foxitcloud.webpdf.common.Signature;
import com.foxitcloud.webpdf.common.WatermarkInfo;


public class FXPDFToImageHandle {
	private long handle = 0;
	
	public FXPDFToImageHandle(long handle)
	{
		this.handle = handle;
	}
	
	protected void finalize() {
		release();
	}
	
	/**
	 * Release all resource.
	 * Note: this function must be call after use FXPDFToImageHandle, otherwise there will be memory leak.
	 */
	public void release()
	{
		Na_release(this.handle);
		this.handle = 0;
	}
	
	/**
	 * @return annotation data of PDF.
	 */
	public String getAnnotData()
	{
		String annotData = Na_getAnnotData(this.handle);
		return annotData;
	}
	
	/**
	 * Parse a PDF document.
	 * @param pdfFilePath path of the document.
	 * @param pdfPassWord password of PDF document.
	 * @return error code.
	 * @see FXPDFError
	 */
	public long parsePDFDocument(String pdfFilePath, String pdfPassWord) {
		   return Na_parsePDFDocument(this.handle, pdfFilePath, pdfPassWord);
	}
	
	/**
	 * Parse a PDF document.
	 * @param pdfStream data of PDF file.
	 * @param pdfPassWord password of PDF document.
	 * @return error code.
	 * @see FXPDFError
	 */
	public long parsePDFDocument(final byte[] pdfStream, String pdfPassWord) {
		   return Na_parsePDFStream(this.handle, pdfStream, pdfPassWord);
	}
	
	/**
	 * Get manifest information of PDF document.
	 * @return manifest information of PDF document.
	 */
	public String getManifestInfo()
	{
		return Na_getManifestInfo(this.handle);
	}
	
	/**
	 * Get document properties of PDF document.
	 * @return document properties of PDF document.
	 */
	public String getDocProperties()
	{
		return Na_getDocProperties(this.handle);
	}
	
	/**
	 * Set DPI of image to generate, default values are 96.
	 * @param horizontalDPI horizontal DPI of image to generate.
	 * @param verticalDPI vertical DPI of image to generate.
	 */
	public void setImageDPI(int horizontalDPI, int verticalDPI)
	{
		Na_setImageDPI(this.handle,horizontalDPI,verticalDPI);
	}
	
	/**
	 * Set max image size to generate.
	 * @param maxWidth max width.
	 * @param maxHeight max height.
	 */
	public void setMaxImageSize(int maxWidth, int maxHeight)
	{
		Na_setMaxImageSize(this.handle,maxWidth,maxHeight);
	}
	
	/**
	 * Get type of PDF document.
	 * @return PDF document type.
	 * @see PDFDocType
	 */
	public int getDocType()
	{
		return Na_getDocType(this.handle);
	}
	
	/**
	 * Get page count of the document.
	 * @return page count of the document.
	 */
	public int	getPageCount()
	{
		return Na_getPageCount(this.handle);
	}
	
	/**
	 * Get bookmark information of the document.
	 * @return bookmark information of the document.
	 */
	public String getBookmarkInfo()
	{
		return Na_getBookmarkInfo(this.handle);
	}
	
	/**
	 * Get page text of the document.
	 * @param pageIndex page index.
	 * @param ret status.
	 * @return page text of the document.
	 * @see FXPDFError
	 */
	public String getPageText(long pageHandle,int pageIndex,MutableLong result)
	{
		Long ret = (long) 0;
		String text = Na_getPageText(this.handle,pageHandle,pageIndex,ret);
		result.setValue(ret);
		if(ret != 0)
			return "";
		return text;		
	}
	
	/**
	 * Render a PDF page to image.
	 * @param pageIndex page index.
	 * @param zoom zoom level, -1 identifies thumb nail. only -1,0.5,1.0,1.5,2.0 supported.
	 * @param isDrawWidget whether display widget.
	 * @param ret status. 
	 * @return image data of the rendered page.
	 * @see FXPDFError
	 */
	public byte[] createPageImage(long pageHandle,int pageIndex,float zoom, boolean isDrawWidget,MutableLong result)
	{
		Long ret = (long) 0;
		byte[] imageStream = Na_createPageImage(this.handle,pageHandle,pageIndex,zoom,isDrawWidget,ret,true);
		result.setValue(ret);
		if(ret != 0)
			return null;
		return imageStream;
	}
	
	/**
	 * Set watermark of image to generate.
	 * @param watermarkInfo watermark information.
	 * @see WatermarkInfo
	 */
	public void setWatermark(WatermarkInfo watermarkInfo)
	{
		if(watermarkInfo == null)
			return;
		Na_setWatermark(watermarkInfo,this.handle);
	}
	
	public long getPageHandle(int pageIndex){
		return Na_getPagehandle(this.handle, pageIndex);
	}
	
	public void releasePageHandle(long pageHandle){
		Na_releasePageHandle(this.handle, pageHandle);
	}
	
	public boolean addSignateure(Signature signature){
		if(null == signature)
			return false;
		
		return Na_addSignatures(handle,signature);
	}
	
	public byte[] getPdfStream(){
		return Na_getPdfStream(handle);
	}
	
	public boolean saveAsPdf(String pdfPath){
		return Na_saveAsFile(handle,pdfPath);
	}
	
	public long getPdfDocHandle(){
		return Na_getDocHandler(handle);
	}
	
	public int  addAnnot(String annotData){
		return Na_addAnnot(handle, annotData);
	}
	
	public String getPdfText(){
		return Na_getPdfText(handle);
	}
	
	protected native void Na_release(long handle);
	protected native String Na_getAnnotData(long handle);
	protected native long Na_parsePDFDocument(long handle,String pdfFilePath, String pdfPassWord);
	protected native long Na_parsePDFStream(long handle,final byte[] pdfStream, String pdfPassWord);
	protected native String Na_getManifestInfo(long handle);
	protected native String Na_getDocProperties(long handle);
	protected native void Na_setImageDPI(long handle,int horizontalDPI, int verticalDPI);
	protected native void Na_setMaxImageSize(long handle,int maxWidth, int maxHeight);
	protected native int Na_getPageCount(long handle);
	protected native int Na_getDocType(long handle);
	protected native String Na_getBookmarkInfo(long handle);
	protected native void Na_setWatermark(WatermarkInfo watermarkInfo, long handle);
	
	protected native String Na_getPageText(long handle,long pageHandle,int pageIndex,Long ret);
	protected native byte[] Na_createPageImage(long handle,long pageHandle,int pageIndex,float zoom, boolean isDrawWidget, Long ret,boolean isTinyPng);

	protected native long Na_getPagehandle(long handle,int pageIndex);
	protected native void Na_releasePageHandle(long handle,long pageHandle);
	
	//protected native Boolean Na_addSignature(long handle,int pageIndex,SignatureData signInfo);
	//protected native Boolean Na_addSignatures(long handle,SignatureItem[] signatureItems);
	protected native boolean Na_addSignatures(long handle,Signature signature);
	protected native byte[]  Na_getPdfStream(long handle);
	protected native boolean Na_saveAsFile(long handle,String pdfPath);
	protected native long  Na_getDocHandler(long handle);
	protected native int   Na_addAnnot(long handle,String annotData);
	protected native String  Na_getPdfText(long handle);
	
}
